from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from myapp.ser import *
from .models import *

# Create your views here.


class GoodsM(APIView):
    def get(self, request):
        data = Goods.objects.all()
        ser = ShowGoodsSer(data, many=True)
        return Response(ser.data)


class ShopM(APIView):
    def post(self, request):
        data = ShopSer(data=request.data)
        if data.is_valid():
            data.save()
            return Response({'code':200})
        else:
            return Response({'code':400})

    def get(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        data = Shop.objects.filter(username=username, password=password).first()
        ser = ShopSer(data)
        return Response(ser.data)


class ShowGoods(APIView):
    def get(self, request):
        data = Goods.objects.all()
        ser = GoodsSer(data, many=True)
        return Response(ser.data)

    def post(self, request):
        data = GoodsSer(data=request.data)
        if data.is_valid():
            data.save()
            return Response({{'code': 200}})
        else:
            return Response({{'code': 400}})

    def delete(self, request):
        id = request.GET.get('id')
        Goods.objects.get(pk=id).delete()
        return Response({'code': 200})
